package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.CoordinaModel;
import models.OrderModel;
import models.OrderProductModel;
import models.RouteModel;
import models.StaffModel;
import admin.data.CoordinaData;
import admin.data.RouteData;
import admin.data.StaffData;
import data.OrderData;

@SuppressWarnings("serial")
public class ViewCoordinaServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setCharacterEncoding("UTF-8");

		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll("/admin/coordina/view/", "");

		CoordinaModel coordinaModel = new CoordinaModel();
		CoordinaData coordinaData = new CoordinaData();
		coordinaModel = coordinaData.getById(params);

		RouteData routeData = new RouteData();
		RouteModel route = routeData.getRoute(coordinaModel.getRouteId());

		StaffData staffData = new StaffData();
		StaffModel staff = staffData.getByCode(coordinaModel.getEmployeeId());

		String[] listOrderId = coordinaModel.getListOrder().split(",");
		TreeMap<String, OrderProductModel> mapOrderProduct = new TreeMap<String, OrderProductModel>();
		List<OrderModel> listOrder = new ArrayList<OrderModel>();
		int totalPrice = 0;
		int expectedPrice = 0;
		int totalVC = 0;
		int totalSP = 0;
		for (int i = 0; i < listOrderId.length; i++) {
			if (listOrderId[i].length() > 9) {
				OrderModel orderModel = OrderData.getOrder(listOrderId[i]);
				listOrder.add(orderModel);
				List<String> listOrderProduct = orderModel.getListOrderProduct();
				for (String stringOrderProduct : listOrderProduct) {
					OrderProductModel orderProduct = new OrderProductModel();
					orderProduct.getValueFromString(stringOrderProduct);
					if (mapOrderProduct
							.containsKey(orderProduct.getProductId())) {
						int quantity = mapOrderProduct.get(
								orderProduct.getProductId()).getQuantity()
								+ orderProduct.getQuantity();
						orderProduct.setStoreType(orderModel
								.getDealProductType());
						orderProduct.setQuantity(quantity);
					} else {
						orderProduct.setStoreType(orderModel
								.getDealProductType());
						if (orderModel.getDealProductType() == 0) {
							totalVC++;
						}
						if (orderModel.getDealProductType() == 1) {
							totalSP++;
						}
					}
					mapOrderProduct.put(orderProduct.getProductId(),orderProduct);
				}
				totalPrice += orderModel.getTotalPrice();
				if (!orderModel.isCheckOut()) {
					expectedPrice += orderModel.getTotalPrice();
				}
			}
		}

		request.setAttribute("coordinaModel", coordinaModel);
		request.setAttribute("route", route);
		request.setAttribute("staff", staff);
		request.setAttribute("mapOrderProduct", mapOrderProduct);
		request.setAttribute("totalPrice", totalPrice);
		request.setAttribute("expectedPrice", expectedPrice);
		request.setAttribute("totalVC", totalVC);
		request.setAttribute("totalSP", totalSP);
		request.setAttribute("listOrder", listOrder);

		try {
			request.getRequestDispatcher("/admin/pages/view_coordina.jsp")
					.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}